# -*- coding: utf-8 -*-
import webapp2
import gaesession
import json
import random
from biyitao.TaobaokeItems import TaobaokeItems as tbk
from BiyiModel.TaoItemModel import TaoItem
from google.appengine.ext import db
class MainPage(webapp2.RequestHandler):
    def get(self):
        keyword = self.request.get('keyword');
        ruobiyi = tbk()
        jsonobj=ruobiyi.getItems(keyword);
        # tao = jsonobj['taobaoke_items_get_response']['taobaoke_items']['taobaoke_item']
        # taoitems = []
        # for t in tao:
        #     item = TaoItem()
        #     item.click_url = t['click_url']
        #     item.num_iid = t['num_iid']
        #     item.title = t['title']
        #     item.price = t['price']
        #     item.nick = t['nick']
        #     item.pic_url = t['pic_url']
        #     item.volume = t['volume']
        #     item.shop_url = t['shop_click_url']
        #     item.wish_count = random.randint(100,300)
        #     taoitems.append(item)
            
        # db.put(taoitems);
        jsontao = json.dumps(jsonobj)
        self.response.headers['Content-Type']='application/json'
        self.response.out.write(str(jsontao).decode('utf-8'))
class AdminItem(webapp2.RequestHandler):
    
    def get(self):
        taoitems = TaoItem.all();
        for item in taoitems.run():
            db.delete(item);
        self.response.out.write(u'已经删除所有数据')

app = webapp2.WSGIApplication([('/xtao',MainPage),('/deletetao',AdminItem)],
                              debug=True,config=gaesession.sessionconfig)